package com.mf.ergate.controller.sys;

import com.mf.ergate.api.sys.DistrictInfoRemoteService;
import com.mf.ergate.api.sys.model.dto.DistrictInfoCreateDto;
import com.mf.ergate.api.sys.model.dto.DistrictInfoDeleteDto;
import com.mf.ergate.api.sys.model.dto.DistrictInfoQueryDto;
import com.mf.ergate.api.sys.model.dto.DistrictInfoUpdateDto;
import com.mf.ergate.api.sys.model.vo.DistrictInfoVo;
import com.mf.ergate.common.utils.MfBeanUtils;
import com.mf.ergate.domain.model.sys.DistrictInfoEntity;
import com.mf.ergate.service.sys.DistrictInfoService;
import com.mf.framework.common.model.Page;
import com.mf.framework.common.model.RemotePage;
import com.mf.framework.common.model.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 行政区信息Controller
 * User: zhaoming
 * DateTime: 2019-03-21
 * To change this template use File | Settings | File Templates.
 */
@RestController
public class DistrictInfoController implements DistrictInfoRemoteService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private DistrictInfoService districtInfoService;

    /**
     * 新增
     * @param districtInfoCreateDto 行政区信息新增Dto
     * @return 行政区信息
     */
    public Result<DistrictInfoVo> insert(@RequestBody DistrictInfoCreateDto districtInfoCreateDto) {
        DistrictInfoEntity districtInfoEntity = MfBeanUtils.copyObject(districtInfoCreateDto, DistrictInfoEntity.class);
        districtInfoEntity = districtInfoService.insert(districtInfoEntity);
        DistrictInfoVo districtInfoVo = MfBeanUtils.copyObject(districtInfoEntity, DistrictInfoVo.class);
        return new Result<>(districtInfoVo);
    }

    /**
     * 根据唯一编号更新
     * @param districtInfoUpdateDto 行政区信息更新Dto
     * @return 执行结果
     */
    public Result<Boolean> updateByUniqueNo(@RequestBody DistrictInfoUpdateDto districtInfoUpdateDto) {
        DistrictInfoEntity districtInfoEntity = MfBeanUtils.copyObject(districtInfoUpdateDto, DistrictInfoEntity.class);
        Boolean flag = districtInfoService.updateByUniqueNo(districtInfoEntity);
        return new Result<>(flag);
    }

    /**
     * 根据唯一编号删除(逻辑删除)
     * @param districtInfoDeleteDto 行政区信息删除Dto
     * @return 结果集
     */
    public Result<Boolean> deleteByUniqueNo(@RequestBody DistrictInfoDeleteDto districtInfoDeleteDto){
        DistrictInfoEntity districtInfoEntity = MfBeanUtils.copyObject(districtInfoDeleteDto, DistrictInfoEntity.class);
        Boolean flag = districtInfoService.deleteByUniqueNo(districtInfoEntity);
        return new Result<>(flag);
    }

    /**
     * 根据唯一编号查询
     * @param uniqueNo 唯一编号
     * @return 行政区信息
     */
    public Result<DistrictInfoVo> selectByUniqueNo(@RequestBody String uniqueNo) {
        DistrictInfoEntity districtInfoEntity = districtInfoService.selectByUniqueNo(uniqueNo);
        DistrictInfoVo districtInfoVo = MfBeanUtils.copyObject(districtInfoEntity, DistrictInfoVo.class);
        return new Result<>(districtInfoVo);
    }

    /**
     * 根据条件查询
     * @param districtInfoQueryDto 行政区信息查询Dto
     * @return 行政区信息列表
     */
    public Result<List<DistrictInfoVo>> selectByCondition(@RequestBody DistrictInfoQueryDto districtInfoQueryDto) {
        List<DistrictInfoEntity> districtInfoEntities = districtInfoService.selectByCondition(districtInfoQueryDto);
        List<DistrictInfoVo> districtInfoVos = MfBeanUtils.copyList(districtInfoEntities, DistrictInfoVo.class);
        return new Result<>(districtInfoVos);
    }

    /**
     * 根据条件查询数量
     * @param districtInfoQueryDto 行政区信息查询Dto
     * @return 数量
     */
    public Result<Integer> selectCount(@RequestBody DistrictInfoQueryDto districtInfoQueryDto) {
        return new Result<>(districtInfoService.selectCount(districtInfoQueryDto));
    }

    /**
     * 根据条件分页查询
     * @param remotePage 远程分页对象
     * @return 行政区信息分页列表
     */
    public Result<Page<DistrictInfoVo>> selectPage(@RequestBody RemotePage<DistrictInfoQueryDto> remotePage){
        Page<DistrictInfoEntity> districtInfoEntityPage = districtInfoService.selectPage(remotePage.getPage(), remotePage.getObj());
        Page<DistrictInfoVo> page = MfBeanUtils.copyPage(districtInfoEntityPage, DistrictInfoVo.class);
        return new Result<>(page);
    }

}